package com.xpgk.mvc.statistic.dailyTotalStatistic;

import java.text.DecimalFormat;
import java.util.List;

import com.jfinal.log.Log;
import com.xpgk.annotation.Service;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.bentryRegister.CustomerAgreement;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.binShopService.CustomerService;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.measurement.Temporary;
import com.xpgk.mvc.productSales.ProductSales;
import com.xpgk.mvc.productSet.Product;
import com.xpgk.tools.ToolDateTime;


@Service(name = DailyTotalStatisticServices.serviceName)
public class DailyTotalStatisticServices extends BaseService {
	
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(DailyTotalStatisticServices.class);
	public static final String serviceName = "dailyTotalStatisticServices";
	
	
	public  void  saveOld()
	{
		/*String  sql  = "SELECT *  FROM  sb_customer_temporary where dataStatus in ('1','2') ";
		List<Temporary>  temporaryList = Temporary.dao.find(sql);
		for(Temporary t:temporaryList)
		{
			//通过测算机构和日期查询日综合查询表数据
			//保存更新顾客统计信息
			DailyNursingTotal dnt = DailyNursingTotal.dao.getDnt(t.getStr(Temporary.column_organIds), t.getDate(Temporary.column_gxsj)!=null ? t.getDate(Temporary.column_gxsj) : t.getDate(Temporary.column_createDate));
			if(dnt!=null)
			{
				dnt.set(DailyNursingTotal.column_zxrs, dnt.getInt(DailyNursingTotal.column_zxrs)+1);//咨询人数
				if(t.getStr(Temporary.column_gkfl).equals(Code.cjCus_ids))
				{
					dnt.set(DailyNursingTotal.column_cjrs, dnt.getInt(DailyNursingTotal.column_cjrs)+1);//成交人数
					double  cjl = ((double)dnt.getInt(DailyNursingTotal.column_cjrs))/((double)dnt.getInt(DailyNursingTotal.column_zxrs));
					dnt.set(DailyNursingTotal.column_xgkCjl,new DecimalFormat("#.00").format(cjl*100));//成交率
					if(t.getStr(Temporary.column_jdfs).equals(Code.zjs_ids))
					{
						dnt.set(DailyNursingTotal.column_zjscjrs, dnt.getInt(DailyNursingTotal.column_zjscjrs)+1);//转介绍成交人数
					}
					else if(t.getStr(Temporary.column_jdfs).equals(Code.xc_ids))
					{
						dnt.set(DailyNursingTotal.column_xccjrs, dnt.getInt(DailyNursingTotal.column_xccjrs)+1);//宣传成交人数
					}else
					{
						dnt.set(DailyNursingTotal.column_zrjdcjrs, dnt.getInt(DailyNursingTotal.column_zrjdcjrs)+1);//自然进店成交人数
					}
				}
				if (!dnt.update()) {
					log.debug("更新日护理综合查询失败！！");
					throw new RuntimeException("更新日护理综合查询失败!");
				}
			}else
			{
				dnt = new DailyNursingTotal();
				dnt.set(DailyNursingTotal.column_organids, t.getStr(Temporary.column_organIds))
					.set(DailyNursingTotal.column_date, ToolDateTime.format(t.getDate(Temporary.column_gxsj)!=null ? t.getDate(Temporary.column_gxsj) : t.getDate(Temporary.column_createDate), ToolDateTime.pattern_ymd))
					.set(DailyNursingTotal.column_zxrs, 1);//咨询人数
				if(t.getStr(Temporary.column_gkfl).equals(Code.cjCus_ids))
				{
					dnt.set(DailyNursingTotal.column_cjrs, 1);//成交人数
					double  cjl = ((double)dnt.getInt(DailyNursingTotal.column_cjrs))/((double)dnt.getInt(DailyNursingTotal.column_zxrs));
					dnt.set(DailyNursingTotal.column_xgkCjl,new DecimalFormat("#.00").format(cjl*100));//成交率
					if(t.getStr(Temporary.column_jdfs).equals(Code.zjs_ids))
					{
						dnt.set(DailyNursingTotal.column_zjscjrs, 1);//转介绍成交人数
					}
					else if(t.getStr(Temporary.column_jdfs).equals(Code.xc_ids))
					{
						dnt.set(DailyNursingTotal.column_xccjrs, 1);//宣传成交人数
					}else
					{
						dnt.set(DailyNursingTotal.column_zrjdcjrs, 1);//自然进店成交人数
					}
				}
				
				if (!dnt.save()) {
					log.debug("保存日护理综合查询失败！！");
					throw new RuntimeException("保存日护理综合查询失败!");
				}		
			}
		}*/	
		
		String cusSql = "SELECT  *  from  sb_customer where dataStatus in ('1','2')";
		List<Customer>  cList = Customer.dao.find(cusSql);
		for(Customer c : cList)
		{	
			//DailyNursingTotal dntLc = DailyNursingTotal.dao.getDnt(c.getStr(Customer.column_organIds),c.getDate(Customer.column_registtime));
			/*if(dntLc != null)
			{
				if(null != c.getCustomerAgreement() && null != c.getCustomerAgreement().getStr(CustomerAgreement.column_codeIds) && c.getCustomerAgreement().getStr(CustomerAgreement.column_codeIds).equals(Code.dlc_ids))
				{
					dntLc.set(DailyNursingTotal.column_xgkDlcs, dntLc.getInt(DailyNursingTotal.column_xgkDlcs)+1);//单疗程数
				}else
				{
					dntLc.set(DailyNursingTotal.column_xgkLcs, dntLc.getInt(DailyNursingTotal.column_xgkLcs)+c.getCustomerAgreement().getInt(CustomerAgreement.column_treatment));//疗程数
				}
				dntLc.set(DailyNursingTotal.column_xgkYjjs,c.getCustomerDetail().getFloat(CustomerDetail.column_delWeight));//应减斤数
				if (!dntLc.update()) {
					log.debug("更新日护理综合查询失败！！");
					throw new RuntimeException("更新日护理综合查询失败!");
				}
			}else
			{	
				dntLc = new DailyNursingTotal();
				dntLc.set(DailyNursingTotal.column_organids, c.getStr(Customer.column_organIds))
					.set(DailyNursingTotal.column_date, ToolDateTime.format(c.getDate(Customer.column_registtime),ToolDateTime.pattern_ymd));
				if(c.getCustomerAgreement() != null && c.getCustomerAgreement().getStr(CustomerAgreement.column_codeIds).equals(Code.dlc_ids))
				{
					dntLc.set(DailyNursingTotal.column_xgkDlcs,1);//单疗程数
				}else
				{
					dntLc.set(DailyNursingTotal.column_xgkLcs, c.getCustomerAgreement() != null ? c.getCustomerAgreement().getInt(CustomerAgreement.column_treatment):0);//疗程数
				}
				dntLc.set(DailyNursingTotal.column_xgkYjjs,c.getCustomerDetail().getFloat(CustomerDetail.column_delWeight));//应减斤数
				if (!dntLc.save()) {
					log.debug("保存日护理综合查询失败！！");
					throw new RuntimeException("保存日护理综合查询失败!");
				}	
			}*/
			
			//保存到店情况
			//根据顾客查询所有到店服务情况
			String serSql = "SELECT *  FROM  sb_customer_service  WHERE  customerID = ? and dataStatus in ('1','2')";
			List<CustomerService>  csList =  CustomerService.dao.find(serSql,c.getStr(Customer.column_ids));
			if(csList != null && csList.size()>0)
			{
				for(CustomerService  cs : csList)
				{
					DailyNursingTotal dntFw = DailyNursingTotal.dao.getDnt(cs.getStr(CustomerService.column_organids), cs.getDate(CustomerService.column_startTime));
					if(dntFw != null)
					{
						dntFw.set(DailyNursingTotal.column_sddrs, dntFw.getInt(DailyNursingTotal.column_sddrs)+1)//实到店人数
							//.set(DailyNursingTotal.column_fwrs, dntFw.getInt(DailyNursingTotal.column_fwrs)+1)//服务人数
							.set(DailyNursingTotal.column_fwcs, dntFw.getInt(DailyNursingTotal.column_fwcs)+1);
						if(null != cs.getFloat(CustomerService.column_weight) && cs.getFloat(CustomerService.column_weight) < Float.parseFloat(c.getCustomerDetail().getStr(CustomerDetail.column_standardWeight)) && null != cs.getFloat(CustomerService.column_lastWeight) && cs.getFloat(CustomerService.column_lastWeight) > Float.parseFloat(c.getCustomerDetail().getStr(CustomerDetail.column_standardWeight)))
						{
							dntFw.set(DailyNursingTotal.column_dbrs, dntFw.getInt(DailyNursingTotal.column_dbrs)+1);
						}
						if (!dntFw.update()) {
							log.debug("保存日护理综合查询失败！！");
							throw new RuntimeException("保存日护理综合查询失败!");
						}
					}else
					{
						dntFw = new DailyNursingTotal();
						dntFw.set(DailyNursingTotal.column_organids, cs.getStr(CustomerService.column_organids))
							.set(DailyNursingTotal.column_date,  ToolDateTime.format(cs.getDate(CustomerService.column_startTime),ToolDateTime.pattern_ymd))
							.set(DailyNursingTotal.column_sddrs, 1)//实到店人数
							.set(DailyNursingTotal.column_fwrs,1)//服务人数
							.set(DailyNursingTotal.column_fwcs,1);
						if(cs.getFloat(CustomerService.column_weight) < Float.parseFloat(c.getCustomerDetail().getStr(CustomerDetail.column_standardWeight)) && cs.getFloat(CustomerService.column_lastWeight) > Float.parseFloat(c.getCustomerDetail().getStr(CustomerDetail.column_standardWeight)))
						{
							dntFw.set(DailyNursingTotal.column_dbrs,1);
						}
						if (!dntFw.save()) {
							log.debug("保存日护理综合查询失败！！");
							throw new RuntimeException("保存日护理综合查询失败!");
						}	
					}
				}
			}
			
			
			//保存销售情况
			
			String  saleSql = "SELECT spsd.*,sp.proBh,sp.typeIds FROM  sb_product_sales_detail  spsd LEFT JOIN  sb_product sp ON  spsd.productID = sp.ids "+
					      " WHERE  customerID = ?";
			List<ProductSales>  psList = ProductSales.dao.find(saleSql,c.getStr(Customer.column_ids));
			if(psList != null )
			{
				for(ProductSales ps : psList)
				{
					DailyNursingTotal dntXs = DailyNursingTotal.dao.getDnt(ps.getStr(ProductSales.column_organids), ps.getDate(ProductSales.column_date));
					if(dntXs != null)
					{	
						dntXs.set(ps.getStr(Product.column_proBh)+"SalNum",dntXs.getInt(ps.getStr(Product.column_proBh)+"SalNum")+ps.getInt(ProductSales.column_number))//产品销售数量
							 .set(ps.getStr(Product.column_proBh)+"SalPrice",dntXs.getDouble(ps.getStr(Product.column_proBh)+"SalPrice")+ps.getFloat(ProductSales.column_price))//产品销售金额
							 .set(DailyNursingTotal.column_priceTotal, dntXs.getDouble(DailyNursingTotal.column_priceTotal)+ps.getFloat(ProductSales.column_price));//销售总金额
						if(ps.getStr(Product.column_typeIds).equals(Code.dlc_ids) || ps.getStr(Product.column_typeIds).equals(Code.lc_ids))
						{
							dntXs.set(DailyNursingTotal.column_jzzxse,dntXs.getDouble(DailyNursingTotal.column_jzzxse)+ps.getFloat(ProductSales.column_price));//减重销售总金额
						}
						if (!dntXs.update()) {
							log.debug("保存日护理综合查询失败！！");
							throw new RuntimeException("保存日护理综合查询失败!");
						}
					}else
					{
						dntXs = new DailyNursingTotal();
						dntXs.set(DailyNursingTotal.column_organids, ps.getStr(ProductSales.column_organids))
							 .set(DailyNursingTotal.column_date,  ToolDateTime.format(ps.getDate(ProductSales.column_date),ToolDateTime.pattern_ymd))
							 .set(ps.getStr(Product.column_proBh)+"SalNum",ps.getInt(ProductSales.column_number))//产品销售数量
							 .set(ps.getStr(Product.column_proBh)+"SalPrice", ps.getFloat(ProductSales.column_price))//产品销售金额
							 .set(DailyNursingTotal.column_priceTotal,ps.getFloat(ProductSales.column_price));//销售总金额
						if(ps.getStr(Product.column_typeIds).equals(Code.dlc_ids) || ps.getStr(Product.column_typeIds).equals(Code.lc_ids))
						{
							dntXs.set(DailyNursingTotal.column_jzzxse,ps.getFloat(ProductSales.column_price));//减重销售总金额
						}
						if (!dntXs.save()) {
							log.debug("保存日护理综合查询失败！！");
							throw new RuntimeException("保存日护理综合查询失败!");
						}
					}
				}
			}
		}
		
	}

}
